package com.cqndt.disaster.device.service.impl;

import java.util.Map;
import java.util.List;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import com.cqndt.disaster.device.dao.*;
import org.springframework.stereotype.Service;
import com.cqndt.disaster.device.vo.TabUserVo;
import com.cqndt.disaster.device.domain.TabStaticType;
import org.springframework.beans.factory.annotation.Autowired;
import com.cqndt.disaster.device.service.DataStatisticsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lgb
 * @date 2020-8-6
 * @说明 数据统计
 **/
@Service
public class DataStatisticsServiceImpl implements DataStatisticsService {
	@Autowired
    private TabBasicMapper tabBasicMapper;
    @Autowired
    private TabProjectMapper tabProjectMapper;
    @Autowired
    private TabMonitorMapper tabMonitorMapper;
    @Autowired
    private TabDeviceMapper tabDeviceMapper;
    @Autowired
    private TabSensorMapper tabSensorMapper;
    @Autowired
    private TabVideoMonitoringMapper tabVideoMonitoringMapper;
    @Autowired
    private TabStaticTypeMapper tabStaticTypeMapper;
    /**
     * @param vo 用户信息
     * @return 基础数据统计
     **/
    @Override
    public Map<String, Object> countBaseNum(TabUserVo vo) {
        Map<String, Object> map = new HashMap<>();
        String areaCodeSplit = vo.getAreaCode().substring(0, 2);
        //灾害点总数
        Integer basicNum=tabBasicMapper.countDisasterNumber(vo.getAreaCode(),vo.getLevel(),areaCodeSplit);
        //项目总数
        Integer projectNum = tabProjectMapper.countProjectNumber(vo.getId());
        //安装布置点总数
        Integer monitorNum = tabMonitorMapper.countMonitorNum(vo.getId());
        //监测设备总数
        Integer deviceNum = tabDeviceMapper.countDeviceNum(vo.getId());
        //传感器总数
        Integer sensorNum = tabSensorMapper.countSensorNum(vo.getId());
        //摄像头总数
        Integer videoNum = tabVideoMonitoringMapper.countVideoNum(vo.getAreaCode(), vo.getLevel(),areaCodeSplit);
        map.put("basicNum", basicNum);
        map.put("projectNum", projectNum);
        map.put("monitorNum", monitorNum);
        map.put("deviceNum", deviceNum);
        map.put("sensorNum", sensorNum);
        map.put("videoNum", videoNum);
        return map;
    }
    /**
     * @param vo
     * @return 灾害点类型统计
     **/
    @Override
    public List<Map<String,Object>> countBasicTypeNum(TabUserVo vo) {
        String areaCodeSplit = vo.getAreaCode().substring(0,2);
        return tabBasicMapper.countDisasterTypeNumber(vo.getAreaCode(),vo.getLevel(),areaCodeSplit);
    }
    @Override
    public List<Map<String,Object>> countDeviceAlarmNum(Integer userId) {
        return tabDeviceMapper.countDeviceAlarmNum(userId);
    }
    /**
     * @param userId
     * @return  设备在线离线统计
     **/
    @Override
    @Transactional(readOnly=true)
    public List<Map<String,Object>> countDeviceStateNum(Integer userId) {
    	List<Map<String,Object>> resultList = new ArrayList<>();
        List<TabStaticType> list = tabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = list.get(0).getStaticKeyval();
        //查询用户对应的所有设备类型
        List<Map<String, Object>> listType = tabDeviceMapper.selectAllType(userId);
        for (Map<String, Object> map : listType) {
            Map<String, Object> resultMap = new HashMap<>();
            Integer zx = tabDeviceMapper.countDeviceStateZx(userId, map.get("device_type").toString(), num);
            Integer lx = tabDeviceMapper.countDeviceStateLx(userId, map.get("device_type").toString(), num);
            resultMap.put("zx", zx);
            resultMap.put("lx", lx);
            resultMap.put("deviceName", map.get("static_name"));
            resultList.add(resultMap);
        }
        return resultList;
    }
    /**
     * @param userId 用户id
     * @return 设备在离线情况
     **/
    @Override
	public List<Map<String,Object>> deviceStateSituation(Integer userId) {
    	List<Map<String,Object>> data = new ArrayList<>();
    	List<TabStaticType> list = tabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = list.get(0).getStaticKeyval();
    	//查询用户对应的所有设备类型
        List<Map<String,Object>> listType = tabDeviceMapper.selectAllType(userId);
        for (Map<String,Object> map : listType) {
        	Map<String,Object> dataMap = new HashMap<>();
            int zx=tabDeviceMapper.countDeviceStateZx(userId,map.get("device_type").toString(),num);
            int lx=tabDeviceMapper.countDeviceStateLx(userId,map.get("device_type").toString(),num);
            int total = zx+lx;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);   
            String zxbf = numberFormat.format((float)zx/(float)total*100);
            dataMap.put("deviceName",map.get("static_name"));
            dataMap.put("totalCount", total);
            dataMap.put("lx", lx);
            dataMap.put("zx", zx);
            dataMap.put("offLinePercentage", zxbf+"%");
            data.add(dataMap);
        }
		return data;
	}
    /**
     * @param userId
     * @return 仪器状态统计（1.1日无数据 2.2日无数据 3.3日无数据 4.4日及以上无数据 5.正常）
     **/
    @Override
    public Map<String,Object> countInstrumentNum(Integer userId) {
        List<TabStaticType> list = tabStaticTypeMapper.selectByStaticNum("11");
        //离线时间判断
        Integer num = list.get(0).getStaticKeyval();
        Map<String,Object> map = tabDeviceMapper.countInstrumentNum(userId,num);
        return map;
    }
    /**
     * @param userId
     * @return 监测类型统计
     **/
    @Override
    public List<Map<String, Object>> countMonitorTypeNum(Integer userId) {
        return tabDeviceMapper.countMonitorTypeNum(userId);
    }
}